package org.airlink.persistence.criteria
{
	import flash.utils.Dictionary;
	
	import flex.persistence.EntityManager;
	import flex.persistence.criteria.AbstractQuery;
	import flex.persistence.criteria.CriteriaQuery;
	import flex.persistence.criteria.Expression;
	import flex.persistence.criteria.Predicate;
	import flex.persistence.criteria.Root;
	import flex.persistence.criteria.Subquery;
	import flex.persistence.metamodel.EntityType;
	
	import org.airlink.persistence.EntityManagerImpl;
	import org.airlink.persistence.EntityTypeImpl;
	
	public class SubQueryImpl extends CriteriaQueryImpl implements Subquery
	{
		private var criteriaQuery:CriteriaQueryImpl;
		
		public function get clazz():Class{
			return super.resultType;
		}
		
		public override function get parameters():Array {
			return criteriaQuery.parameters;
		}
		
		public function toString():String{
			return this.asValue();
		}
		
		public function asValue():String{
			return this.entityManager.createSqlQuery(this);
			
		}
		
		public function SubQueryImpl(resultClass:Class, criteriaQuery:CriteriaQueryImpl, criteriaBuilder:CriteriaBuilderImpl)
		{	
			super(resultClass, criteriaQuery.entityManager as EntityManagerImpl, criteriaQuery.transaction,criteriaBuilder);
			this.criteriaQuery = criteriaQuery;
			this._parameters = criteriaQuery.parameters;
		}
		
		//override pour reprendre l'aliasIndex de la criteriaQuery mère
		override public function from(entityClass:Class):Root {		
			var entityType:EntityType = new EntityTypeImpl(entityClass);			
			var root:Root = new RootImpl(entityType, entityManager as EntityManagerImpl, criteriaQuery.aliasIndex);			
			super.addRoot(root);
			return root;
		}


		
			
	}
}